25 April, 2019
The “fillna” function in Pandas can replace missing values with a given constant value.
Example:
import pandas as pd
import numpy as np
df = pd.DataFrame([[np.nan], [2], [np.nan], [0]])
df
This is the dataframe with few missing values.
Output:
0 | |
---|---|
0 | NaN |
1 | 2.0 |
2 | NaN |
3 | 0.0 |
Example:
Replace the missing values by value ‘47’
df.fillna(47)
Output:
0 | |
---|---|
0 | 47.0 |
1 | 2.0 |
2 | 47.0 |
3 | 0.0 |
You can also replace a missing value with the next (or previous) value in the data frame!
Example:
Replace missing value with previous value
df.fillna(method=”ffill”)
Output:
0 | |
---|---|
0 | NaN |
1 | 2.0 |
2 | 2.0 |
3 | 0.0 |
Note that the first value cannot be replaced because nothing is preceding it.
You can also use the value of the next row to fill a missing value.
Example:
Replace missing value with next available value
df.fillna(method=”bfill”)
Output:
0 | |
---|---|
0 | 2.0 |
1 | 2.0 |
2 | 0.0 |
3 | 0.0 |